package com.sl.core.workflow;

import cn.hutool.core.util.BooleanUtil;
import com.sl.core.engine.ProcessConfig;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.meta.trans.ProcessTransDescriptor;
import com.sl.core.engine.step.impl.AbstractProcessComp;
import com.sl.core.engine.trans.ProcessInstance;
import org.apache.hop.pipeline.transform.ITransformMeta;
import org.apache.hop.pipeline.transform.TransformMetaDataCombi;

import java.util.List;

/**
 * todo workflow 待实现
 */
public class WorkFlowInstance extends ProcessInstance {
    public WorkFlowInstance(ProcessTransDescriptor transMeta, ProcessConfig processConfig) {
        super(transMeta, processConfig);
    }


    /**
     * 0 执行中 1.执行失败 2执行成功
     * @return
     */
    @Override
    public Integer isEnd() {

        //判断是否是走完了
        List<TransformMetaDataCombi> transforms = getTransforms();

        long count = transforms.stream().filter(v -> {
            if (v.transform instanceof AbstractProcessComp<?, ?> a) {
                ITransformMeta meta = a.getMeta();

                if (meta instanceof AbstractProcessCompDescriptor p) {
                    if (BooleanUtil.isTrue(p.getIsEndStep())) {
                        return true;
                    }
                }
            }
            return false;
        }).filter(v -> {
            return v.transform.getLinesRead() > 0;
        }).count();
        if (count <= 0) {
            return 0;
        }
        return this.getErrors() > 0 ? 1 : 2;
    }
}
